// 引入mockjs
import { _ } from "core-js";
import Mock from "mockjs";

const translateUrl = function(url) {
  let arr = url.split("?")[1].split("&"); //先通过？分解得到？后面的所需字符串，再将其通过&分解开存放在数组里
  let obj = {};
  for (let i of arr) {
    obj[i.split("=")[0]] = i.split("=")[1]; //对数组每项用=分解开，=前为对象属性名，=后为属性值
  }
  return obj;
};

const random = Mock.Random;

const responseData = {
  success: true,
  data: Object,
  msg: null,
};

const _user = {
  name: "@cname",
  userName: "@word",
  pwd: "@string(5)",
  "role|1": ["manager", "user"],
  tel: "@zip",
  "status|1": [0, 1],
};

const _project = {
  id: "@natural",
  Title: "@word",
  TotalPrice: "@float",
  StartDate: "@datetime",
  EndDate: "@datetime",
  Organization: "旷世科技",
  Address: "@county(true)",
  Contacter: "@cname",
  Tel: "@zip",
  "Status|1": [0, 1, 2],
};

const _payment = {
  id: "@natural",
  Msg: "@cparagraph",
  Expense: "@float(0,50000,3,2)",
  Time: "@datetime",
  User: _user,
  "State|1": [0, 1, 2],
};

const _parametric = {
  id: "@natural",
  Key: "@ctitle",
  Value: "@csentence",
};

const _report = {
  id: "@natural",
  UserName: "@word",
  Title: "@ctitle",
  Content: "@csentence",
  Time: "@datetime",
};

const _attachment = {
  id: "@natural",
  FileInfoId: "@natural",
  "FileName|1": ["1.jpg", "asda.pdf", "554dsa.doc", "666.xls"],
};

// ------------------登录----------------------

Mock.mock(/\/api\/account\/login/, "post", (req) => {
  const { username, password } = JSON.parse(req.body);
  console.log("login", username, password);
  const result = { ...responseData };
  if (username) {
    result.data = {
      user: Mock.mock(_user),
      token: random.guid(),
    };
  } else {
    result.success = false;
    result.msg = "username is missing!";
  }
  return result;
});

// ------------------项目----------------------
Mock.mock(/\/api\/project\/list/, "get", (req) => {
  const { name, page, size } = translateUrl(req.url);
  console.log("mock", name, page, size);
  const list = [];
  for (let i = (page - 1) * size; i < page * size; i++) {
    const project = { ..._project };

    project.Attachments = [];
    for (let j = 0; j < random.integer(0, 5); j++) {
      project.Attachments.push(_attachment);
    }

    list.push(Mock.mock(project));
  }
  const result = { ...responseData };
  result.data = {
    list,
    totalCount: 45,
  };
  return result;
});

Mock.mock(/\/api\/project/, "get", (req) => {
  const { id } = translateUrl(req.url);
  console.log("mock", id);

  const project = { ..._project };
  project.Users = [];
  for (let j = 0; j < random.integer(1, 5); j++) {
    project.Users.push(Mock.mock(_user));
  }
  project.Payments = [];
  for (let j = 0; j < random.integer(0, 5); j++) {
    project.Payments.push(Mock.mock(_payment));
  }
  project.Parametrics = [];
  for (let j = 0; j < random.integer(0, 10); j++) {
    project.Parametrics.push(Mock.mock(_parametric));
  }
  project.Reports = [];
  for (let j = 0; j < random.integer(0, 5); j++) {
    const report = { ..._report };
    report.User = Mock.mock(_user);
    project.Reports.push(Mock.mock(report));
  }
  project.Attachments = [];
  for (let j = 0; j < random.integer(0, 5); j++) {
    project.Attachments.push(_attachment);
  }
  const result = { ...responseData };
  result.data = Mock.mock(project);
  return result;
});

Mock.mock(/\/api\/project/, "post", (req) => {
  const formData = req.body;
  const project = { ..._project };
  _.forOwn(project, function(value, key) {
    project[key] = formData.get(key);
  });

  project.Attachments = [];
  _.each(formData.getAll("Files"), () => {
    project.Attachments.push(_attachment);
  });

  const result = { ...responseData };
  result.data = Mock.mock(project);
  console.log(result);
  return result;
});

Mock.mock(/\/api\/project/, "put", (req) => {
  const formData = req.body;
  const project = { ..._project };
  _.forOwn(project, function(value, key) {
    project[key] = formData.get(key);
  });

  project.Attachments = [];
  _.each(formData.getAll("Files"), () => {
    project.Attachments.push(_attachment);
  });

  const result = { ...responseData };
  result.data = Mock.mock(project);
  return result;
});

Mock.mock(/\/api\/project/, "delete", (req) => {
  const { id } = translateUrl(req.url);
  const result = { ...responseData };
  if (id) {
    result.data = Mock.mock(_project);
  } else {
    result.success = false;
    result.msg = "主键丢失";
  }
  return result;
});

// ------------------用户----------------------
Mock.mock(/\/api\/user\/list/, "get", (req) => {
  const { name, userName, role, page, size } = translateUrl(req.url);
  console.log("mock", name, userName, role, page, size);
  const list = [];
  for (let i = (page - 1) * size; i < page * size; i++) {
    list.push(Mock.mock(_user));
  }
  const result = { ...responseData };
  result.data = {
    list,
    totalCount: 45,
  };
  return result;
});

Mock.mock(/\/api\/user/, "post", (req) => {
  const { name, userName, pwd, role, Tel } = JSON.parse(req.body);
  const result = { ...responseData };
  const user = { ..._user, name, userName, pwd, role, Tel };
  result.data = Mock.mock(user);
  return result;
});

Mock.mock(/\/api\/user/, "put", (req) => {
  const { id, name, userName, pwd, role, Tel } = JSON.parse(req.body);
  const result = { ...responseData };
  const user = { ..._user, id, name, userName, pwd, role, Tel };
  result.data = Mock.mock(user);
  return result;
});

Mock.mock(/\/api\/user/, "delete", (req) => {
  const { id } = translateUrl(req.url);
  const result = { ...responseData };
  if (id) {
    const user = { ..._user, id };
    result.data = Mock.mock(user);
  } else {
    result.success = false;
    result.msg = "主键丢失";
  }
  return result;
});

// ------------------统计----------------------
Mock.mock(/\/api\/statistic\/dashboard/, "get", () => {
  const result = { ...responseData };
  result.data = {
    totalProject: 124,
    waitProject: 20,
    doingProject: 87,
    overProject: 17,
  };
  return result;
});

Mock.mock(/\/api\/statistic\/status/, "get", () => {
  const result = { ...responseData };
  result.data = [
    { status: 0, count: random.integer(0, 127) },
    { status: 1, count: random.integer(0, 127) },
    { status: 2, count: random.integer(0, 127) },
  ];
  return result;
});

function genArray(n) {
  let res = [];
  for (let i = 0; i < n; i++) {
    res.push(random.integer(0, 127));
  }
  return res;
}

Mock.mock(/\/api\/statistic\/monthStatus/, "get", () => {
  const result = { ...responseData };
  result.data = [
    { type: 0, data: genArray(12) },
    { type: 1, data: genArray(12) },
    { type: 2, data: genArray(12) },
  ];
  return result;
});

console.log("%c前端 mock 环境启动成功", "color: #38f;font-weight: bold");

export default Mock;
